#!/usr/bin/perl -w

use strict;
use warnings;

my $root = `pwd`;
$root =~ s/\s*$//;
my $root_reg = $root;
$root_reg =~ s/\//\\\//g;
print STDERR "\$root_tpp_galaxy_tools=".$root."\n";
#grep -R "my \$root_tpp_galaxy_tools" ./*| grep -v 'svn.base'|awk -F":" '{print $1}'
my $cmd_find_scripts = "grep -R \"my \\\$root_tpp_galaxy_tools\" ./*| grep -v 'svn-base'|awk -F\":\" '{print \$1}'| grep '.pl\$'";
#print $cmd_find_scripts."\n";
#print `$cmd_find_scripts`; 
my @scripts = split /\n/,`$cmd_find_scripts`;# get related perl scripts
foreach (@scripts){
    #chop; 
    my $script = $_;
    print STDERR "Set \$root_tpp_galaxy_tools for script:".$script."\n";
#sed -r 's/(my\s\$abc\s=\s").*(".*$)/\1abc\2/' abc.txt    
    my $cmd_set_root = "sed -r \'s/(my \\\$root_tpp_galaxy_tools = \").*(\".*\$)/\\1$root_reg\\2/\' $script";
    #print $cmd_set_root."\n";
    #print `$cmd_set_root`; die;
    my $tmp_script = `$cmd_set_root`;
    open (my $HTEMP,">",$script);
    print $HTEMP $tmp_script;
    close $HTEMP;    
}

